/**
 * 
 */
package com.huawei.esdk.uc.sametime.popup.actions;

import org.apache.log4j.Logger;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IWorkbenchPart;

import com.huawei.esdk.uc.sametime.ConstantsDefines;
import com.huawei.esdk.uc.sametime.ConstantsDefines.CallStatus;
import com.huawei.esdk.uc.sametime.international.Messages;
import com.huawei.esdk.uc.sametime.services.UCCallService;
import com.huawei.esdk.uc.sametime.services.UCSettingsService;
import com.huawei.esdk.uc.sametime.util.GlobalConfiguration;
import com.huawei.esdk.uc.sametime.widgets.CallMemuUtil;
import com.ibm.collaboration.realtime.livenames.PersonSelection;
import com.ibm.collaboration.realtime.people.PeopleUtil;
import com.ibm.collaboration.realtime.people.Person;
import com.ibm.rcp.realtime.livenames.LiveName;

/**
 * 上下文菜单中的视频呼叫功能控制类 Action which creates a video call cascading menu for a
 * single person selection.
 * @author y00175154
 */
public class EsdkVideoCallLiveNameActionDelegate extends
        AbstractObjectActionDelegate
{
    private static Logger logger = Logger
            .getLogger(EsdkVideoCallLiveNameActionDelegate.class);

    private CallMemuUtil menuCreator;

    private LiveName[] livename;

    public void setActivePart(IAction action, IWorkbenchPart targetPart)
    {
        action.setMenuCreator(getClickToCallContextMenuCreator());
    }

    private CallMemuUtil getClickToCallContextMenuCreator()
    {
        if (null == menuCreator)
        {
            menuCreator = new CallMemuUtil(true, true);
        }
        return menuCreator;
    }

    public void selectionChanged(IAction action, ISelection selection)
    {
        super.selectionChanged(action, selection);

        boolean isSamtimeLogin = GlobalConfiguration.getInstance()
                .isSamtimeLogin();
        boolean isUCLogin = GlobalConfiguration.getInstance().isUCLogin();
        boolean isInCall = (CallStatus.end != UCCallService.getCallStatus());
        boolean isIPPhone = (ConstantsDefines.IP_PHONE_MODE.equals(UCSettingsService.getPhoneJoinDev()));

        if (isSamtimeLogin && isUCLogin && !isInCall && !isIPPhone)
        {
            livename = getSelectedLivenames();
            // 检查是否只选中了一个人做视频呼叫
            if (livename.length == 1)
            {
                logger.debug(menuCreator
                        + " selection changed. Current person is "
                        + livename[0].getDisplayName());

                if (!PeopleUtil.isMe(((PersonSelection) livename[0])
                        .getPerson()))
                {
                    menuCreator.setSelectionPerson((Person) livename[0]);
                    action.setEnabled(true);
                    return;
                }
            }
        }
        action.setEnabled(false);
    }

    public void run(IAction action)
    {
        // 插件运行异常报错提示
        MessageDialog.openError(null,
                Messages.EsdkVideoCallLiveNameActionDelegate_EXCEPTION_ERROR,
                Messages.EsdkVideoCallLiveNameActionDelegate_PLUGIN_ERROR);
    }

}
